public class 最大宽度坡 {

    public static int MAXN = 50001;
    public static int[] stack = new int[MAXN];
    public static int r;
    public int maxWidthRamp(int[] nums) {
        int n = nums.length;
        r = 1;
        int ans = 0;
        for (int i = 1; i < n; i++) {
            if (nums[stack[r - 1]] > nums[i]) {
                stack[r++] = i;
            }
        }
        for (int i = n - 1; i >= 0; i--) {
            while (r > 1 && nums[stack[r - 1]] >= nums[i]) {
                int cur = stack[--r];
                ans = Math.max(ans,i - cur);
            }

        }
        return ans;
    }
}
